using UnityEngine;
using System.Collections;

public class ColorPanel : MonoBehaviour 
{
	public ColorFactory Factory;
	private Color32 color;
	private static ArrayList componentsList = new ArrayList();
			
	void OnCollisionEnter(Collision other)
	{
		if(other.gameObject.tag == "Objeto")
		{
			if(other.gameObject.name != "Triangulo(Clone)")
			{
				color = other.gameObject.renderer.material.color;
			}
			else
			{
				color = other.gameObject.transform.GetChild(0).renderer.material.color;
			}
			
			renderer.materials[1].color = Factory.setColor(color);
			
			if(!componentsList.Contains(other.gameObject))
			{
				componentsList.Add(other.gameObject);
			}
		}
	}
	
	void OnCollisionExit(Collision other)
	{
		if(other.gameObject.tag == "Objeto")
		{
			if(other.gameObject.name != "Triangulo(Clone)")
			{
				color = other.transform.renderer.material.color;
			}
			else
			{
				color = other.gameObject.transform.GetChild(0).renderer.material.color;
			}
			
			renderer.materials[1].color = Factory.removeColor(color);
			
			componentsList.Remove(other.gameObject);
		}
	}
	
	public static void DestroyComponents()
	{
		foreach (GameObject go in componentsList) 
		{
			Destroy(go);
		}
		
		componentsList.Clear();
	}
}
